#include "copy.hpp"

#include "qbe/src/ecs/app.hpp"

#include "qbe/src/ecs/query.hpp"

#include "qbe/src/axis/axis.hpp"
#include "qbe/src/input/input.hpp"
#include "qbe/src/select/select.hpp"
#include "qbe/src/state/state.hpp"
#include "qbe/src/undoRedo/undoRedo.hpp"
#include "qbe/src/utils/utils.hpp"

#include "qbe/src/layout/layout.hpp"

#include "qlib/all.h"

#include "../affix/affix.hpp"
#include "components.hpp"

namespace qbe::copy {

// 退出模式时回调
void exit_callback(ecs::Query query, state::AllModeType mode) {
  // 获取移动参数
  auto& param = query.get_resource<Param>();

  // 获取reg
  auto& cell = query.get_resource<qlib::Cell::s_ptr>();
  auto& reg = cell->reg;

  //   // 如果进入粘贴
  //   if (state::isModeType<state::EditingModeType>(mode) && state::getModeType<state::EditingModeType>(mode) == state::EditingModeType::Affix) {
  //     // 重置参数
  //     param.reset();
  //   } else {
  //     // 重置参数
  //     param.reset();

  //     // 清除标记
  //     auto view = reg.view<CopyTag>();
  //     view.each([&reg](entt::entity e) { reg.remove<CopyTag>(e); });
  //   }

  //   param.reset();
}

void init(ecs::Commands commands, ecs::Query query) {
  Param param;
  commands.insert_resource(param);

  auto& state = query.get_resource<state::State>();
  state.addExitCall(state::EditingModeType::Copy, exit_callback);
}

void SetCopy(ecs::Query query) {
  auto& cell = query.get_resource<qlib::Cell::s_ptr>();
  auto& reg = cell->reg;

  // 移除之前的对象
  auto view_old = reg.view<CopyTag>();
  view_old.each([&reg](entt::entity e) { reg.remove<CopyTag>(e); });

  // 遍历选中对象
  auto view = reg.view<select::SelectTag>();

  // 添加拷贝标记
  view.each([&reg](entt::entity e) { reg.emplace_or_replace<CopyTag>(e); });

  auto& param = query.get_resource<Param>();

  // 包含拷贝对象
  param.reg = &reg;
  param.cell = cell;
  param.enable = true;
  param.query = query.get_resource<ecs::Query*>();

  auto& param_affix = query.get_resource<affix::Param>();
  param_affix.from_copy = true;
}

// 拷贝
void update(ecs::Query query, ecs::Bus& bus) {
  // 判断编辑模式
  auto& state = query.get_resource<state::State>();

  if (state.getEditingMode() != state::EditingModeType::Copy)
    return;

  // 获取cell和reg
  auto& cell = query.get_resource<qlib::Cell::s_ptr>();
  auto& reg = cell->reg;
  auto& layout_cmd = query.get_resource<layout::LayoutCmd>();

  // 获取输入 坐标系 撤销恢复
  auto& handModule = query.get_resource<vsg::ref_ptr<input::HandleBundle>>();
  auto& axisModule = query.get_resource<axis::Axis>();
  auto& undoRedoModule = query.get_resource<undoRedoT::UndoRedoModule>();

  // 获取移动参数
  auto& param = query.get_resource<Param>();

  // 移除之前的对象
  auto view_old = reg.view<CopyTag>();
  view_old.each([&reg](entt::entity e) { reg.remove<CopyTag>(e); });

  // 遍历选中对象
  auto view = reg.view<select::SelectTag>();

  // 添加拷贝标记
  view.each([&reg](entt::entity e) { reg.emplace_or_replace<CopyTag>(e); });

  // 包含拷贝对象
  param.reg = &reg;
  param.cell = cell;
  param.enable = true;
  param.query = query.get_resource<ecs::Query*>();

  auto& param_affix = query.get_resource<affix::Param>();
  param_affix.from_copy = true;

  // 退出模式
  state.exit();
}

void Plugin::build(ecs::App& app) {
  app.add_stage<InitStage>();
  app.add_stage<UpdateStage>();

  app.add_systems<InitStage>(init);
  app.add_systems<UpdateStage>(update);
}

}  // namespace qbe::copy